/*    
    OpenMheg - A free MHEG-5 library
    Copyright (C) 2008 Dharmesh Malam

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;
using Core.Model.Groups;
using Core.Model.Ingredients;
using XMHEGDecoder.Builders.Visibles;

namespace XMHEGDecoder.Builders
{
    public class GroupBuilder
    {

        public static void Build ( XmlElement node, Group group )
        {
            //  Build parent class
            RootBuilder.Build( node ["root"], group );

            #region  Build our attributes

            XmlElement e;

            e = node ["standard-version"];
            if ( e != null )
                group.StandardVersion = DecodeUtils.ParseInt( e.InnerText );

            e = node ["object-information"];
            if ( e != null )
                group.ObjectInformation = DecodeUtils.DecodeBase64( e.InnerText );

            e = node ["on-start-up"];
            if ( e != null )
                group.OnStartUp = ActionBuilder.Build( e );

            e = node ["on-close-down"];
            if ( e != null )
                group.OnCloseDown = ActionBuilder.Build( e );

            e = node ["original-group-cache-priority"];
            if ( e != null )
                group.OriginalGroupCachePriority = DecodeUtils.ParseInt( e.InnerText );

            e = node ["items"];
            if ( e != null )
                group.Items = BuildItems( e );

            #endregion
        }

        public static List<Ingredient> BuildItems ( XmlElement node )
        {
            List<Ingredient> ret = new List<Ingredient>();
            XmlNodeList nodes = node.ChildNodes;

            foreach ( XmlElement ingredient in nodes )
            {
                Ingredient item = BuildItem( ingredient );
                ret.Add( item );
            }

            return ret;
        }

        private static Ingredient BuildItem ( XmlElement node )
        {
            Ingredient ret = null;
            
            switch ( node.Name )
            {

                case ( "resident-program" ):

                    ret = ResidentProgramBuilder.Build( node );
                    break;

                case ( "boolean-variable" ):

                    ret = VariableBuilder.BuildBoolean( node );
                    break;
                case ( "integer-variable" ):

                    ret = VariableBuilder.BuildInteger( node );
                    break;
                case ( "octet-string-variable" ):

                    ret = VariableBuilder.BuildOctetString( node );
                    break;
                case ( "object-ref-variable" ):

                    ret = VariableBuilder.BuildObjectRef( node );
                    break;
                case ( "content-ref-variable" ):

                    ret = VariableBuilder.BuildContentRef( node );
                    break;
                case ( "link" ):

                    ret = LinkBuilder.Build( node );
                    break;
                case ( "stream" ):
                    
                    ret = StreamBuilder.Build( node );
                    break;
                case ( "bitmap" ):

                    ret = BitmapBuilder.Build( node );
                    break;
                case ( "dynamic-line-art" ):

                    ret = DynamicLineArtBuilder.Build( node );
                    break;
                case ( "rectangle" ):

                    ret = RectangleBuilder.Build( node );
                    break;
                case ( "text" ):

                    ret = TextBuilder.Build( node );
                    break;
                case ( "entry-field" ):

                    
                    //ret = TextBuilder.Build( node );
                    //log.warning( "EntryField not yet implemented - returning a text field" );

                    break;
                case ( "hyper-text" ):

                    
                    //ret = TextBuilder.Build( node );
                    //log.warning( "HyperText not yet implemented - returning a text field" );
                    break;
                case ( "slider" ):

                    //log.warning( "Slider not yet implemented" );
                    break;
                case ( "token-group" ):

                    ret = TokenGroupBuilder.Build( node );
                    break;
                case ( "list-group" ):

                    break;

                default:
                    ret = null;
                    throw new Exception( "Unknown Item" );

            }

            if ( ret == null )
                throw new Exception( "Unknown item" );

            return ret;
        }
    }
}
